//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Users;
using DotNetNuke.Security;
using DotNetNuke.Security.Membership;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.Log.EventLog;
using DotNetNuke.Services.Mail;
namespace DotNetNuke.Modules.Admin.Security
{
	partial class SendPassword : UserModuleBase
	{
		private string ipAddress;
		private UserInfo _User;
		private int _UserCount = Null.NullInteger;
		protected string RedirectURL
		{
			get
			{
				string _RedirectURL = "";
				if (Request.QueryString["returnurl"] != null)
				{
					_RedirectURL = HttpUtility.UrlDecode(Request.QueryString["returnurl"]);
					if (_RedirectURL.Contains("://"))
					{
						_RedirectURL = "";
					}
					if (_RedirectURL.Contains("?returnurl"))
					{
						string baseURL = _RedirectURL.Substring(0, _RedirectURL.IndexOf("?returnurl"));
						string returnURL = _RedirectURL.Substring(_RedirectURL.IndexOf("?returnurl") + 11);
						_RedirectURL = string.Concat(baseURL, "?returnurl", HttpUtility.UrlEncode(returnURL));
					}
				}
				if (String.IsNullOrEmpty(_RedirectURL))
				{
					_RedirectURL = Common.Globals.NavigateURL();
				}
				return _RedirectURL;
			}
		}
		protected bool UseCaptcha
		{
			get
			{
				object setting = UserModuleBase.GetSetting(PortalId, "Security_CaptchaLogin");
				return Convert.ToBoolean(setting);
			}
		}
		private void GetUser()
		{
			ArrayList arrUsers;
			if (MembershipProviderConfig.RequiresUniqueEmail && !String.IsNullOrEmpty(txtEmail.Text.Trim()) && String.IsNullOrEmpty(txtUsername.Text.Trim()))
			{
				arrUsers = UserController.GetUsersByEmail(PortalSettings.PortalId, txtEmail.Text, 0, Int32.MaxValue, ref _UserCount);
				if (arrUsers != null && arrUsers.Count == 1)
				{
					_User = (UserInfo)arrUsers[0];
				}
			}
			else
			{
				_User = UserController.GetUserByName(PortalSettings.PortalId, txtUsername.Text);
			}
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			bool isEnabled = true;
			if (MembershipProviderConfig.PasswordRetrievalEnabled)
			{
				lblHelp.Text = Localization.GetString("SendPasswordHelp", LocalResourceFile);
				cmdSendPassword.Text = Localization.GetString("SendPassword", LocalResourceFile);
				cmdSendPassword.ImageUrl = "~/images/password.gif";
			}
			else if (MembershipProviderConfig.PasswordResetEnabled)
			{
				lblHelp.Text = Localization.GetString("ResetPasswordHelp", LocalResourceFile);
				cmdSendPassword.Text = Localization.GetString("ResetPassword", LocalResourceFile);
				cmdSendPassword.ImageUrl = "~/images/reset.gif";
			}
			else
			{
				isEnabled = false;
				lblHelp.Text = Localization.GetString("DisabledPasswordHelp", LocalResourceFile);
				tblSendPassword.Visible = false;
			}
			if (MembershipProviderConfig.RequiresUniqueEmail && isEnabled)
			{
				lblHelp.Text += Localization.GetString("RequiresUniqueEmail", LocalResourceFile);
			}
			if (MembershipProviderConfig.RequiresQuestionAndAnswer && isEnabled)
			{
				lblHelp.Text += Localization.GetString("RequiresQuestionAndAnswer", LocalResourceFile);
			}
		}
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdLogin.Click += new EventHandler(cmdLogin_Click);
			cmdSendPassword.Click += new EventHandler(cmdSendPassword_Click);

			if (Request.UserHostAddress != null)
			{
				ipAddress = Request.UserHostAddress;
			}
			rowEmailLabel.Visible = MembershipProviderConfig.RequiresUniqueEmail;
			rowEmailText.Visible = MembershipProviderConfig.RequiresUniqueEmail;
			trCaptcha1.Visible = UseCaptcha;
			trCaptcha2.Visible = UseCaptcha;
			if (UseCaptcha)
			{
				ctlCaptcha.ErrorMessage = Localization.GetString("InvalidCaptcha", this.LocalResourceFile);
				ctlCaptcha.Text = Localization.GetString("CaptchaText", this.LocalResourceFile);
			}
		}
		protected void cmdLogin_Click(object sender, System.EventArgs e)
		{
			Response.Redirect(RedirectURL, true);
		}
		private void cmdSendPassword_Click(System.Object sender, EventArgs e)
		{
			string strMessage = Null.NullString;
			string strLogMessage = Null.NullString;
			bool canSend = true;
			if (MembershipProviderConfig.RequiresQuestionAndAnswer && String.IsNullOrEmpty(txtAnswer.Text))
			{
				GetUser();
				if (_User != null)
				{
					lblQuestion.Text = _User.Membership.PasswordQuestion;
				}
				tblQA.Visible = true;
				return;
			}
			if ((UseCaptcha && ctlCaptcha.IsValid) || (!UseCaptcha))
			{
				if (String.IsNullOrEmpty(txtUsername.Text.Trim()))
				{
					if (MembershipProviderConfig.RequiresUniqueEmail)
					{
						if (String.IsNullOrEmpty(txtEmail.Text.Trim()))
						{
							canSend = false;
							strMessage = Services.Localization.Localization.GetString("EnterUsernameEmail", this.LocalResourceFile);
						}
					}
					else
					{
						canSend = false;
						strMessage = Services.Localization.Localization.GetString("EnterUsername", this.LocalResourceFile);
					}
				}
				if (canSend)
				{
					PortalSecurity objSecurity = new PortalSecurity();
					GetUser();
					if (_User != null)
					{
						if (_User.IsDeleted)
						{
							canSend = false;
							strMessage = Localization.GetString("UsernameError", this.LocalResourceFile);
						}
						else
						{
							if (MembershipProviderConfig.PasswordRetrievalEnabled)
							{
								try
								{
									_User.Membership.Password = UserController.GetPassword(ref _User, txtAnswer.Text);
								}
								catch (Exception ex)
								{
									canSend = false;
									strMessage = Localization.GetString("PasswordRetrievalError", this.LocalResourceFile);
								}
							}
							else
							{
								try
								{
									_User.Membership.Password = UserController.GeneratePassword();
									UserController.ResetPassword(_User, txtAnswer.Text);
								}
								catch (Exception ex)
								{
									canSend = false;
									strMessage = Localization.GetString("PasswordResetError", this.LocalResourceFile);
								}
							}
							if (canSend)
							{
								if (Mail.SendMail(_User, MessageType.PasswordReminder, PortalSettings) != string.Empty)
								{
									strMessage = Localization.GetString("SendMailError", this.LocalResourceFile);
									canSend = false;
								}
								else
								{
									strMessage = Localization.GetString("PasswordSent", this.LocalResourceFile);
								}
							}
						}
					}
					else
					{
						if (_UserCount > 1)
						{
							strMessage = Localization.GetString("MultipleUsers", this.LocalResourceFile);
						}
						else if (MembershipProviderConfig.RequiresUniqueEmail && !String.IsNullOrEmpty(txtEmail.Text.Trim()) && String.IsNullOrEmpty(txtUsername.Text.Trim()))
						{
							strMessage = Localization.GetString("EmailError", this.LocalResourceFile);
						}
						else
						{
							strMessage = Localization.GetString("UsernameError", this.LocalResourceFile);
						}
						canSend = false;
					}
					if (canSend)
					{
						Services.Log.EventLog.EventLogController objEventLog = new Services.Log.EventLog.EventLogController();
						Services.Log.EventLog.LogInfo objEventLogInfo = new Services.Log.EventLog.LogInfo();
						objEventLogInfo.AddProperty("IP", ipAddress);
						objEventLogInfo.LogPortalID = PortalSettings.PortalId;
						objEventLogInfo.LogPortalName = PortalSettings.PortalName;
						objEventLogInfo.LogUserID = UserId;
						objEventLogInfo.LogUserName = objSecurity.InputFilter(txtUsername.Text, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoAngleBrackets | PortalSecurity.FilterFlag.NoMarkup);
						objEventLogInfo.LogTypeKey = "PASSWORD_SENT_SUCCESS";
						objEventLog.AddLog(objEventLogInfo);
						UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.GreenSuccess);
					}
					else
					{
						Services.Log.EventLog.EventLogController objEventLog = new Services.Log.EventLog.EventLogController();
						Services.Log.EventLog.LogInfo objEventLogInfo = new Services.Log.EventLog.LogInfo();
						objEventLogInfo.AddProperty("IP", ipAddress);
						objEventLogInfo.LogPortalID = PortalSettings.PortalId;
						objEventLogInfo.LogPortalName = PortalSettings.PortalName;
						objEventLogInfo.LogUserID = UserId;
						objEventLogInfo.LogUserName = objSecurity.InputFilter(txtUsername.Text, PortalSecurity.FilterFlag.NoScripting | PortalSecurity.FilterFlag.NoAngleBrackets | PortalSecurity.FilterFlag.NoMarkup);
						objEventLogInfo.LogTypeKey = "PASSWORD_SENT_FAILURE";
						objEventLogInfo.LogProperties.Add(new LogDetailInfo("Cause", strMessage));
						objEventLog.AddLog(objEventLogInfo);
						UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
					}
					cmdLogin.Visible = true;
				}
				else
				{
					UI.Skins.Skin.AddModuleMessage(this, strMessage, UI.Skins.Controls.ModuleMessage.ModuleMessageType.RedError);
				}
			}
		}
	}
}
